package de.home.develop.firstgame.logic
{
	import de.home.develop.firstgame.logic.GameObjectManager;
	
	import flash.events.*;
	import flash.geom.*;
	import flash.net.Socket;
	import flash.ui.Keyboard;
	
	import mx.core.*;
	import mx.events.MoveEvent;
	
		
	public class Player extends AnimatedGameObject
	{
		
			
		protected var jumpAnimationTime:Number = 0.40;
		protected var jumping:Boolean = false
		protected var jumpLock:Number = 0;
		protected var playerType:Number;
		protected var mousePosX:Number; 
		protected var movingLeft:Boolean = false;
		protected var movingRight:Boolean = false;
		protected var connection:Socket;
		protected var query:String;
		protected var startPosition:Number;
		protected var oldPosition:Point =  new Point();
		
		public static const MOUSE:Number = 0;
		
		private static const COCKMONGLER:Number = 1;		
		
				
		
		public function Player(playerType:Number, position:Number)
		{
			this.startPosition = position;
			this.playerType = playerType;	
		}
		
		public function startupPlayer():void		
		{			
			super.startupAnimatedGameObject(ResourceManager.PlayerGraphics, new Point(startPosition,	440), ZOrders.PlayerZOrder);						
			this.collisionName = CollisionIdentifiers.PLAYER;
			this.connection = GameObjectManager.getNc();
			this.oldPosition.x = -1;
			this.oldPosition.y = -1;
			
		}
		
		override public function shutdown():void		
		{			
			super.shutdown();			
		}
				
		
		
		//Bewegt den Spieler, in einem bestimmten Intervall X Pixel auf der x-Achse
		private function movePlayer():void
		{
			if(playerType == MOUSE)
			{				
				if(mousePosX > position.x)
				{
					position.x += 5;
					if(position.x > mousePosX)
					{
						position.x = mousePosX;
						
					}				
				}
				
				if(mousePosX < position.x)
				{
					position.x -= 5;
					if(position.x < mousePosX)
					{
						position.x = mousePosX;
					}
				}		
			}
			if(playerType == COCKMONGLER)
			{
				if(movingLeft)
				{
					position.x -= 5;
					
				}
				
				if(movingRight)
				{
					position.x += 5;
					
				}
				
								
			}
			
			if(position.x <= 0)
			{
				position.x = 5;
			}
			
			
			if(position.x >= FlexGlobals.topLevelApplication.width - graphics.bitmap.width / graphics.frames)
			{
				position.x = FlexGlobals.topLevelApplication.width - graphics.bitmap.width / graphics.frames;
			}
		}
		
		private function jumpPlayer(dt:Number):void
		{
			if(jumping)
			{
				jumpAnimationTime -= dt;
				if(jumpAnimationTime >= 0)
				{
					position.y -= 8;
					
				}else
				{
					jumping = false;
					jumpAnimationTime = 0.40;									
				}
				
			}else
			{
				if(position.y < 440)
				{
					position.y += 8;
					if(position.y == 440)
					{
						jumpLock--;
					}
				}
			}
		}
		
		
		override public function enterFrame(dt:Number):void		
		{						
			if (inuse)		
			{				
				frameTime += dt;				
				if (graphics.fps != -1)					
				{
					
					while (frameTime > 1/graphics.fps)						
					{
						
						frameTime -= 1/graphics.fps;
						//todo fuer keyboard spieler auch implementieren
						if((playerType == MOUSE && position.x == mousePosX) || (playerType == COCKMONGLER && !movingLeft && !movingRight))
						{
							currentFrame = 	2;							
						}
						else{
							currentFrame = (currentFrame + 1) % graphics.frames;	
						}
												
						if (currentFrame == 0 && playOnce)							
						{							
							shutdown();							
							break;						
						}					
					}				
				}			
			}												
			jumpPlayer(dt);
			if(((oldPosition.x != position.x) || (oldPosition.y != position.y))  && (playerType == MOUSE))
			{
				if(connection != null)
				{
					
					this.connection.writeUTFBytes(CommandHelper.writePosition(position.x,position.y));
					
					this.connection.flush();
											
				}
				this.oldPosition.x = position.x;
				this.oldPosition.y = position.y;				
			}
			
			query = "";	
			movePlayer();										
		
		}
		
		override public function mouseMove(event:MouseEvent):void			
		{
			if(event.stageX >= 0 && event.stageX <= FlexGlobals.topLevelApplication.width)
			{
						
				mousePosX = event.stageX;
				// move player to mouse position														
				// keep player on the screen			
				if (position.x < 0)				
					position.x = 0;
			
				if (position.x >= FlexGlobals.topLevelApplication.width - (graphics.bitmap.width / graphics.frames))
				{
					position.x = FlexGlobals.topLevelApplication.width - (graphics.bitmap.width / graphics.frames);
				
				}
			}		
			
		}
		
		override public function mouseDown(event:MouseEvent):void		
		{			
			if(playerType == MOUSE)
			{
				
			
				if(jumpLock == 0)
				{
					jumping = true;
					jumpLock++;
				}
			}			
		}
		
	
		
		override public function mouseUp(event:MouseEvent):void		
		{			
			
		}		
						
		override public function collision(other:GameObject):void		
		{						
		}					
		
	
		private function randomNumber(low:Number=0, high:Number=1):Number
		{
			return Math.floor(Math.random() * (1+high-low)) + low;
		}
		
		
	}
}